home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8207 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.0 KB  |  50 lines

  1. Path: sun001.spd.dsccc.com!spd!jmccarty
  2. From: jmccarty@spd.dsccc.com (Mike McCarty)
  3. Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland
  4. Subject: Re: Tough FACTORIAL math problem...
  5. Date: 15 Feb 1996 19:15:26 GMT
  6. Organization: DSC Communications Corporation, Plano, Texas USA
  7. Message-ID: <4g00oe$ghe@sun001.spd.dsccc.com>
  8. References: <4fr8be$ass@news.iconn.net> <4ft2uv$575@crl.crl.com>
  9. NNTP-Posting-Host: aplo139.spd.dsccc.com
  10.  
  11. In article <4ft2uv$575@crl.crl.com>, Robert Fry <bobfry@crl.com> wrote:
  12. )thecrow@iconn.net (The Crow) writes:
  13. )
  14. )>Here is what I am trying to do, I want to find the last NON-ZERO digit of a 
  15. )>given factorial.  For instance,
  16. )
  17. )>5! = 120
  18. )
  19. )>so the last non-zero digit is 2.  I want to be able to do this up to 1000.  
  20. )>Problem is, no matter how huge of a data-type you use, there isn't any way for 
  21. )>the computer to handle 1000!, it is however possible to find the last non-zero 
  22. )>digit somehow.  One thing I have tried is as I went through mulitplying the 
  23. )>series of numbers in the factorial (5 * 4 * 3 * 2) I would remove all the 
  24. )>trailing ZEROS, I got this to work up to 789, but it wont work with 1000 and i 
  25. )>am not really sure why.  If anyone has a clue how I can do this let me know.
  26. )
  27. )As you already know, the number of digits in a factorial number grows 
  28. )very quickly. I calculated and printed out 1000! several years ago, and 
  29. )it more than fills a page with digits. However, since all you want to do 
  30. )is keep track of the last non-zero digit, why don't you simply perform 
  31. )the multiplications and keep just the last digit? I.e. 3!=6, 
  32. )4!=4*3!=4*6=24 and then just store the 4.
  33. )
  34. )This way you could use plain ints or longs, and your interim result 
  35. )(before dropping down to the last nonzero digit) will never be greater 
  36. )than 9x for x!.
  37.  
  38. Nice try, but no cigar. 
  39.  
  40.     4! = 24 ~ 4
  41.     5! = 5x4! ~ 5x4 = 20 ~ 0
  42.  
  43. Now you no longer have the last NON ZERO digit.
  44.  
  45. Mike
  46. ----
  47. char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
  48.  
  49. I don't speak for DSC.         <- They make me say that.
  50.